<?php
/**
 * Created by PhpStorm.
 * User: HXL
 * Date: 2017/6/1
 * Time: 15:43
 */
namespace app\order\model;

use think\Db;

class Clients extends BaseModel{
    protected $tableName = 'ari_clients';

    public function selectDistinct($field, $where){
        $result = Db::table($this->tableName)->field($field)->where($where)->distinct(true)->select();
        return $result;
    }

    /**
     * 模糊查询
     * @param $search
     * @param $tableName2
     * @param $param
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function searchLike($search, $tableName2, $param){
        $count = Db::table($this->tableName)->join($tableName2, $this->tableName.'.'.$param.'='.$tableName2.'.'.$param)
            ->where('cid',  'like', "%$search%")->whereOr('client', 'like', "%$search%")->whereOr('state', 'like', "%$search%")->count();
        $client = Db::table($this->tableName)->join($tableName2, $this->tableName.'.'.$param.'='.$tableName2.'.'.$param)
            ->where('cid',  'like', "%$search%")->whereOr('client', 'like', "%$search%")->whereOr('state', 'like', "%$search%")
            ->paginate(10, $count, ['query' => request()->param()]);
        return $client;
    }

    /**
     * 联合查询
     * @param $tableName2
     * @param $param
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function join($tableName2, $param, $where){
        $client = Db::table($this->tableName)->join($tableName2, $this->tableName.'.'.$param.'='.$tableName2.'.'.$param)->where($where)->select();
        return $client;
    }

    /**
     *
     */
    public function joinSid($where){
        $client = Db::table($this->tableName)
            ->join('ari_states state', $this->tableName.'.sid=state.sid')->where($where)->find();
        return $client;
    }

    public function page($field = '*', $where = array(), $order = '')
    {
        $num = 10;
        $join = array(
            ['ari_states sta', $this->tableName.'.sid=sta.sid']
        );
        $count = Db::table($this->tableName)->join($join)->where($where)->count();
        $data = Db::table($this->tableName)->field($field)->where($where)
            ->order($order)
            ->join($join)
            ->paginate($num, $count, ['query' => request()->param()]);
        return $data;
    }


}